package com.ruoyi.flow.service.impl;

import com.gitee.deeround.jdbchelper.DbAction;
import com.ruoyi.core.entity.UserContext;
import com.ruoyi.core.util.IdUtil;
import com.ruoyi.flow.domain.Instance;
import com.ruoyi.flow.domain.Trans;
import com.ruoyi.flow.entity.FireContext;
import com.ruoyi.flow.entity.machine.Flow;
import com.ruoyi.flow.service.FlowInstanceService;
import com.ruoyi.flow.service.FlowUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FlowInstanceServiceImpl implements FlowInstanceService {

    @Autowired
    private FlowUserService flowUserService;

    @Autowired
    private DbAction dbAction;

    @Override
    public Instance getInstance(String instanceId) {

        String sql = "select * from flow_ru_instance where instance_id=?";

        return dbAction.queryForObject(sql, Instance.class, instanceId);
    }

    @Override
    public List<Trans> getTransList(String instanceId) {
        String sql = "select * from flow_ru_trans where instance_id=? order by trans_id asc";

        return dbAction.queryForListObject(sql, Trans.class, instanceId);
    }

    @Override
    public List<Trans> getTransLogList(String instanceId) {
        String sql = "select * from flow_ru_trans_log where instance_id=? order by trans_id asc";

        return dbAction.queryForListObject(sql, Trans.class, instanceId);
    }

    @Override
    public Instance addInstance(Flow flow) {

        Instance entity = new Instance();
        entity.setInstanceId(IdUtil.sequenceId());
        entity.setMachineId(flow.getId());

        String sql = "insert into flow_ru_instance(instance_id,machine_id,source_node_id,target_node_id,line_id,event_id,create_dept,create_by,create_time,update_by,update_time) values(?,?,?,?,?,?,?,?,?,?,?)";

        UserContext userContext = flowUserService.currentUser();
        Date now = new Date();
        dbAction.update(sql, new Object[]{

                entity.getInstanceId(),
                entity.getMachineId(),
                entity.getSourceNodeId(),
                entity.getTargetNodeId(),
                entity.getLineId(),
                entity.getEventId(),
                userContext.getDeptId(),
                userContext.getUserId(),
                now,
                userContext.getUserId(),
                now

        });

        return entity;
    }

    @Override
    public Trans addTransLog(FireContext fireContext) {

        Trans entity = new Trans();
        entity.setTransId(IdUtil.sequenceId());
        entity.setInstanceId(fireContext.getInstance().getInstanceId());
        entity.setSourceNodeId(fireContext.getSource().getId());
        entity.setTargetNodeId(fireContext.getTarget().getId());
        entity.setLineId(fireContext.getLine() == null ? null : fireContext.getLine().getId());
        entity.setEventId(fireContext.getEvent().getId());
        entity.setOpinion(fireContext.getOpinion());
        entity.setSourceNodeName(fireContext.getSource().getName());
        entity.setTargetNodeName(fireContext.getTarget().getName());
        entity.setEventName(fireContext.getEvent().getName());

        String sql = "insert into flow_ru_trans_log(trans_id,instance_id,source_node_id,target_node_id,line_id,event_id,opinion,source_node_name,target_node_name,event_name,create_dept,create_by,create_time,update_by,update_time) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        UserContext userContext = flowUserService.currentUser();
        Date now = new Date();
        dbAction.update(sql, new Object[]{

                entity.getTransId(),
                entity.getInstanceId(),
                entity.getSourceNodeId(),
                entity.getTargetNodeId(),
                entity.getLineId(),
                entity.getEventId(),
                entity.getOpinion(),
                entity.getSourceNodeName(),
                entity.getTargetNodeName(),
                entity.getEventName(),
                userContext.getDeptId(),
                userContext.getUserId(),
                now,
                userContext.getUserId(),
                now

        });

        return entity;
    }

    @Override
    public Trans addTrans(FireContext fireContext) {

        Trans entity = new Trans();
        entity.setTransId(IdUtil.sequenceId());
        entity.setInstanceId(fireContext.getInstance().getInstanceId());
        entity.setSourceNodeId(fireContext.getSource().getId());
        entity.setTargetNodeId(fireContext.getTarget().getId());
        entity.setLineId(fireContext.getLine() == null ? null : fireContext.getLine().getId());
        entity.setEventId(fireContext.getEvent().getId());
        entity.setOpinion(fireContext.getOpinion());
        entity.setSourceNodeName(fireContext.getSource().getName());
        entity.setTargetNodeName(fireContext.getTarget().getName());
        entity.setEventName(fireContext.getEvent().getName());

        String sql = "insert into flow_ru_trans(trans_id,instance_id,source_node_id,target_node_id,line_id,event_id,opinion,source_node_name,target_node_name,event_name,create_dept,create_by,create_time,update_by,update_time) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        UserContext userContext = flowUserService.currentUser();
        Date now = new Date();
        dbAction.update(sql, new Object[]{

                entity.getTransId(),
                entity.getInstanceId(),
                entity.getSourceNodeId(),
                entity.getTargetNodeId(),
                entity.getLineId(),
                entity.getEventId(),
                entity.getOpinion(),
                entity.getSourceNodeName(),
                entity.getTargetNodeName(),
                entity.getEventName(),
                userContext.getDeptId(),
                userContext.getUserId(),
                now,
                userContext.getUserId(),
                now

        });

        return entity;
    }

    @Override
    public void clearTrans(String instanceId) {
        String sql = "delete from flow_ru_trans where instance_id=?";

        dbAction.update(sql, new Object[]{instanceId});
    }

    @Override
    public void deleteTrans(String transId) {
        String sql = "delete from flow_ru_trans where trans_id=?";

        dbAction.update(sql, new Object[]{transId});
    }

    @Override
    public void updateInstance(String instanceId) {

        //将流转表最近一条数据上的：source_node_id、target_node_id、line_id、event_id更新到流程实例上

        String sql = "select * from flow_ru_trans where instance_id=? order by trans_id desc";
        Trans trans = dbAction.queryForObject(sql, Trans.class, instanceId);

        sql = "update flow_ru_instance set source_node_id=?,target_node_id=?,line_id=?,event_id=?,source_node_name=?,target_node_name=?,event_name=?,update_by=?,update_time=? where instance_id=?";

        UserContext userContext = flowUserService.currentUser();
        Date now = new Date();
        dbAction.update(sql,
                new Object[]{
                        trans.getSourceNodeId(),
                        trans.getTargetNodeId(),
                        trans.getLineId(),
                        trans.getEventId(),
                        trans.getSourceNodeName(),
                        trans.getTargetNodeName(),
                        trans.getEventName(),
                        userContext.getUserId(),
                        now,
                        trans.getInstanceId(),
                });

    }
}
